//
//  ChatTableViewCell.m
//  HXSDK
//
//  Created by dllo on 16/8/17.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "ChatTableViewCell.h"
#import <AudioUnit/AudioUnit.h>
#import <AVFoundation/AVFoundation.h>
@interface ChatTableViewCell ()
/**时间*/
@property (nonatomic, strong) UILabel *timeLabel;
/**聊天信息*/
@property (nonatomic, strong) UIButton *chatBtn;
/**头像*/
@property (nonatomic, strong) UIButton *iconBtn;
/**姓名*/
@property (nonatomic, strong) UILabel *chatterLabel;
/**真实高度*/
@property (nonatomic, assign) CGSize realSize;

@property (retain, nonatomic) AVAudioPlayer *audioPlayer;

@end
@implementation ChatTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.timeLabel = [[UILabel alloc] init];
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_timeLabel];
        
        self.chatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _chatBtn.backgroundColor = [UIColor magentaColor];
        [self addSubview:_chatBtn];
        [_chatBtn addTarget:self action:@selector(chatAction) forControlEvents:UIControlEventTouchUpInside];
        
        self.iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_iconBtn];
        
        self.chatterLabel = [[UILabel alloc] init];
        _timeLabel.textAlignment = NSTextAlignmentLeft;
        [self addSubview:_chatterLabel];
        
    }
    return self;
}
-(void)chatAction{
    id body = _message.messageBodies[0];
    if ([body isKindOfClass:[EMVoiceMessageBody class]]) {
        EMVoiceMessageBody *voiceBody = body;
        NSString *path = voiceBody.localPath;
        NSFileManager *manager = [NSFileManager defaultManager];
        if (![manager fileExistsAtPath:path]) {
            //从远程服务器获取地址
            path = voiceBody.remotePath;
          
        }
        
        NSLog(@"ppath : %@", path);
        _audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:path] error:nil];
        _audioPlayer.volume = 1.0;
        [_audioPlayer prepareToPlay];
        [_audioPlayer play];

    }else if ([body isKindOfClass:[EMVideoMessageBody class]]){
        
    }else{
        
    }
    
}

-(void)layoutSubviews{
    [super layoutSubviews];
    _timeLabel.frame = CGRectMake(WIDTH - 50, 0, 100, 20);
    
}
-(void)setMessage:(EMMessage *)message{
    _message = message;
//    EMTextMessageBody; 文本消息体
//    EMImageMessagebody 图片消息体
//    EMVideoMessageBody 视频消息体
//    EMVoiceMessageBody 声音消息体
    //获取消息体
    id body = message.messageBodies[0];
    if ([body isKindOfClass:[EMTextMessageBody class]]) {
         EMTextMessageBody *textBody = body;
        [self.chatBtn setTitle:textBody.text forState:UIControlStateNormal];
        _chatBtn.titleLabel.numberOfLines = 0;
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        self.realSize = [textBody.text boundingRectWithSize:CGSizeMake(WIDTH / 2,  MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

    }else if ([body isKindOfClass:[EMVoiceMessageBody class]]){
        EMVoiceMessageBody *voiceBody = body;
        UIImageView *imageV = [[UIImageView alloc]init];
        imageV.image = [UIImage imageNamed:@"语音"];
        UILabel *label = [[UILabel alloc] init];
        label.text = [NSString stringWithFormat:@"%ld",(long)voiceBody.duration];
        imageV.frame = CGRectMake(0, 0, 50, 20);
        label.frame = CGRectMake(60, 0, 20, 20);
        [_chatBtn addSubview:label];
        [_chatBtn addSubview:imageV];
        _realSize = CGSizeMake(80, 20);
        
    }else{
        
    }
    self.iconBtn.backgroundColor = [UIColor orangeColor];
       NSString *chatter = [[EaseMob sharedInstance].chatManager loginInfo][@"username"];
    //头像
      [self.iconBtn setImage:[UIImage imageNamed:@"beijin"] forState:UIControlStateNormal];
    /**/
    //消息是谁发送的
    if ([message.from isEqualToString:chatter]) {//自己发送的
        self.chatterLabel.text = message.from;
        //头像在右边
        _iconBtn.frame = CGRectMake(WIDTH - 60, 20, 50, 50);
        //聊天消息在左边
        _chatBtn.frame = CGRectMake(WIDTH - _realSize.width - 65, 40, _realSize.width, _realSize.height + 20);
        //名称在右边
        _chatterLabel.frame = CGRectMake(WIDTH - 100, 20, 50, 20);
        
    }else//别人发的, 头像在左边
    {
         self.chatterLabel.text = message.from;
        //头像在左边
        _iconBtn.frame = CGRectMake(10, 20, 50, 50);
        //聊天消息在右边
        _chatBtn.frame = CGRectMake(70, 40, _realSize.width, _realSize.height + 20);
        //名称在左边
        _chatterLabel.frame = CGRectMake(55, 20, 50, 20);
    }
   

}
- (CGFloat) rowHeight{
    return 100 + _rowHeight;
}

@end
